$('.brand_txt').hover(function () {
    $('.top_brand').css('display', 'flex')
    $('.brand_txt').css({ 'border': '1px solid #bbbbbb', 'border-bottom': 'none', 'background-color': '#fff', 'color': '#ee0055', })
}, function () {
    $('.top_brand').css('display', 'none')
    $('.brand_txt').css({ 'border': 'none', 'background-color': '#eaeaea', 'color': '#000' })
})
$('.top_brand').hover(function () {
    $('.top_brand').css('display', 'flex')
    $('.brand_txt').css({ 'border': '1px solid #bbbbbb', 'border-bottom': 'none', 'background-color': '#fff', 'color': '#ee0055' })
}, function () {
    $('.top_brand').css('display', 'none')
    $('.brand_txt').css({ 'border': 'none', 'background-color': '#eaeaea', 'color': '#000' })
})
//cookie
function Cookie() { };
Cookie.prototype.set = function (key, value, expires) {
    var data = new Date();
    data.setSeconds(data.getSeconds() + expires);
    document.cookie = key + '=' + value + ';expires=' + data.toUTCString();
}
Cookie.prototype.get = function (key) {
    var cookie = document.cookie;
    var arr = cookie.split('; ');
    var obj = {};
    arr.forEach(item => {
        var str = item.replace(/=/, '*a*')
        var arr = str.split('*a*')
        obj[arr[0]] = arr[1]
    })
    return obj[key]
}
Cookie.prototype.remove = function (key) {
    this.set(key, '', -1)
}

//nav_轮播
var mySwiper1 = new Swiper('.swiper-container1', {
    autoplay: {
        delay: 3000,//3秒切换一次
    },
    loop: true,
    navigation: {
        nextEl: '.nav_carousel_r',
        prevEl: '.nav_carousel_l',
    },
    pagination: {
        el: '.nav_swiper-pagination',
        clickable: true,
    }
})
// 鼠标移入停止自动滚动
$('.swiper-container1').mouseenter(function () {
    mySwiper1.autoplay.stop();
})
// 鼠标移出开始自动滚动
$('.swiper-container1').mouseleave(function () {
    mySwiper1.autoplay.start();
})
//轮播图动态渲染
var carousel = document.querySelectorAll('.carousel_img')
$.get("http://127.0.0.1:8000/index_images/", {}, function (data) {
    for (let i = 0; i < data.data.length; i++) {
        $(carousel[0]).attr('src', data.data[data.data.length - 1].url)
        $(carousel[data.data.length + 1]).attr('src', data.data[0].url)
        $(carousel[i + 1]).attr('src', data.data[i].url)
    }
});
//品牌左
$.get('http://127.0.0.1:8000/categorys/', {}, function (data) {
    var brand_nav
    for (let i = 0; i < data.data.length; i++) {
        brand_clone = $(".brand_clone").clone();
        brand_clone.removeClass('brand_clone')
        brand_clone.css('display', 'block')
        $('.nav_cont').append(brand_clone)
        $('.brand_nav b').eq(i + 1).text(data.data[i].name)
        for (let j = 0; j < data.data[i].childs.length; j++) {
            let a = $("<a href='javascript:;'>" + data.data[i].childs[j].name + "</a>")
            $(brand_clone.children()[1]).append(a)
        }
    }
})
//品牌图
$.get('http://127.0.0.1:8000/brands/', {}, function (data) {
    let arr = []
    for (let i = 0; i < data.data.length; i++) {
        if (data.data[i].logo.indexOf('h', 0) === 0) {
            arr.push(data.data[i].logo)
        }
    }
    for (let i = 0; i < arr.length; i++) {
        let str = arr[i]
        str = str.substring(str.lastIndexOf('http'), str.length)
        $('.brand_bx img').eq(i).attr('src', str)
    }
})
//搜索
$('.search_btn').click(function () {
    var search = $('.head_search > input').val();
    var c = new Cookie();
    c.set('q', search)
    location.href = 'http://127.0.0.1:5500/practise/mzsc_new/html/Product.li.html#q=' + search;
    $('.head_search > input').val('');
});
document.addEventListener('keydown', function (e) {
    if (e.key == 'Enter') {
        var search = $('.head_search > input').val();
        var c = new Cookie();
        c.set('q', search)
        location.href = 'http://127.0.0.1:5500/practise/mzsc_new/html/Product.li.html#q=' + search;
        $('.head_search > input').val('');
    }
})